//
//  InformationListHeadView.m
//  TeamInformatization
//
//  Created by 李光磊 on 2019/4/2.
//  Copyright © 2019 SPIC. All rights reserved.
//

#import "InformationListHeadView.h"

@interface InformationListHeadView ()<UITextFieldDelegate>

@property (nonatomic, strong) NSString *searchStr;

// 标签数组
@property (nonatomic, strong) NSArray *markArray;

// 标签字典
@property (nonatomic, strong) NSDictionary *markDict;

// 选中标签数组(数字)
@property (nonatomic, strong) NSMutableArray *selectedMarkArray;

// 选中标签数组(文字字符串)
@property (nonatomic, strong) NSMutableArray *selectedMarkStrArray;

@property (nonatomic, strong) NSMutableArray *xArr;

@end

@implementation InformationListHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor whiteColor];
        
        [self setupUI];
    }
    return self;
}
-(NSMutableArray *)xArr {
    if (!_xArr) {
        _xArr = [NSMutableArray arrayWithObjects:@15,@95,@175, nil];
    }
    return _xArr;
}
#pragma mark - 懒加载
- (NSArray *)markArray {
    if (!_markArray) {
        NSArray *array = [NSArray array];
        array = @[@"管理规范", @"操作规程", @"工作票"];
        _markArray = array;
    }
    return _markArray;
}

// 上传通过文字key取数字value发送数字
- (NSDictionary *)markDict {
    if (!_markDict) {
        NSDictionary *dict = [NSDictionary dictionary];
        dict = @{
                 @"管理规范" : @"3" ,
                 @"操作规程" : @"7",
                 @"工作票" : @"9",
                 };
        _markDict = dict;
    }
    return _markDict;
}

- (NSMutableArray *)selectedMarkArray {
    if (!_selectedMarkArray) {
        _selectedMarkArray = [NSMutableArray array];
    }
    return _selectedMarkArray;
}

- (NSMutableArray *)selectedMarkStrArray {
    if (!_selectedMarkStrArray) {
        _selectedMarkStrArray = [NSMutableArray array];
    }
    return _selectedMarkStrArray;
}

-(void)setupUI {
    
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(15,15,kScreenWidth-30,36);
    view.layer.borderWidth = 1;
    view.layer.borderColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0].CGColor;
    view.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    view.layer.cornerRadius = 4;
    [self addSubview:view];
    
    //nametext
    UITextField *searchText = [[UITextField alloc] init];
    searchText.font = kFont(PFSCRegular, 12);
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"工作票管理规范" attributes:
                                      @{NSForegroundColorAttributeName:kUIColorHex(0x8A8A8A),
                                        NSFontAttributeName:kFont(PFSCRegular, 12)
                                        }];
    searchText.delegate = self;
    searchText.attributedPlaceholder = attrString;
    [searchText addTarget:self action:@selector(searchText:) forControlEvents:UIControlEventAllEvents];
    [self addSubview:searchText];
    [searchText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(0);
        make.left.equalTo(view.mas_left).offset(13);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(36);
    }];
    
    UIButton *searchBtn = [[UIButton alloc] init];
    [searchBtn setImage:[UIImage imageNamed:@"分组 10"] forState:UIControlStateNormal];
    [self addSubview:searchBtn];
    [searchBtn addTarget:self action:@selector(searchBtn:) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(0);
        make.left.equalTo(view.mas_right).offset(-36);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(36);
    }];
    
    for (NSInteger i = 0; i < 3; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.clipsToBounds = YES;
        btn.layer.borderWidth = 1.0f;
        btn.layer.borderColor = kUIColorHex(0xF1F1F1).CGColor;
        [btn setTitleColor:kUIColorHex(0x8A8A8A) forState:UIControlStateNormal];
        [btn setTitle:@"管理规范" forState:UIControlStateNormal];
        btn.titleLabel.font = kFont(PFSCRegular, 12);
        btn.layer.cornerRadius = 4;
        [btn addTarget:self action:@selector(chooseMark:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat xfloat = [self.xArr[i] floatValue];
        btn.frame = CGRectMake(xfloat, view.bottom+15, 70, 26);
        [btn setTitle:self.markArray[i] forState:UIControlStateNormal];
        [self addSubview:btn];
    }

}
-(void)searchText:(UITextField *)textfield {
    _searchStr =[NSString stringWithFormat:@"%@",textfield.text];
}
-(void)searchBtn:(UIButton *)sender {
    [self endEditing:YES];
    self.searchBlock(_searchStr);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}
- (void)chooseMark:(UIButton *)btn {
    
    btn.selected = !btn.selected;
    
    if (btn.isSelected) {
        btn.layer.borderColor = kUIColorHex(0x576B95).CGColor;
        [btn setTitleColor:kUIColorHex(0x576B95) forState:UIControlStateNormal];
        [self.selectedMarkArray addObject:self.markDict[btn.titleLabel.text]];
        [self.selectedMarkStrArray addObject:btn.titleLabel.text];
    } else {
        btn.layer.borderColor = kUIColorHex(0xF1F1F1).CGColor;
        [btn setTitleColor:kUIColorHex(0x8A8A8A) forState:UIControlStateNormal];
        [self.selectedMarkArray removeObject:self.markDict[btn.titleLabel.text]];
        [self.selectedMarkStrArray removeObject:btn.titleLabel.text];
    }
}
@end
